package _8nio;

import org.junit.Test;

import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

/**
 * @description:
 * @author: wang zhen
 * @create: 2022-07-29 11:24
 */
public class ZeroCopyTest {

    @Test
    public void mmapTest() throws IOException {

        FileChannel fileChannel=FileChannel.open(Paths.get("C:\\Users\\test\\Desktop\\aaa.jpeg"), StandardOpenOption.READ);
        MappedByteBuffer map = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());

        FileChannel writeChannel = FileChannel.open(Paths.get("C:\\Users\\test\\Desktop\\bbb.jpeg"), StandardOpenOption.CREATE,StandardOpenOption.WRITE);

        //数据传输
        writeChannel.write(map);
        System.out.println("写入完成");

        fileChannel.close();
        writeChannel.close();


    }



    @Test
    public void sendFileTest() throws IOException {

        FileChannel fileChannel=FileChannel.open(Paths.get("C:\\Users\\test\\Desktop\\aaa.jpeg"), StandardOpenOption.READ);

        FileChannel writeChannel = FileChannel.open(Paths.get("C:\\Users\\test\\Desktop\\bbb.jpeg"), StandardOpenOption.CREATE,StandardOpenOption.WRITE);

        //数据传输
        fileChannel.transferTo(0,fileChannel.size(),writeChannel);
        System.out.println("写入完成");

        fileChannel.close();
        writeChannel.close();


    }
}
